﻿using System;

using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Reflection;
using System.Windows.Forms;
using System.Collections;
using ScannerApp.ScannerWebserviceWrapper;

namespace ScannerApp.Utils
{

    public enum ScanBarcodeStatus 
    {
        None = 0, 
        InStockCode, 
        OutStockCode,
        TransferStockCode,
        ProductCode, 
        ShelfCode, 
        ProductCode_SecondPart,
        DeliveryOrderCode
    };



    public class DataTableFieldAttribute : System.Attribute
    {
        private string headerText;

        public string HeaderText
        {
            get { return headerText; }
            set { headerText = value; }
        }

        private int width = 80;

        public int Width
        {
            get { return width; }
            set { width = value; }
        }

    }

    public class CheckBarcodeKeys
    {
        public static string BarcodeType = "BarcodeType";
        public static string ProductCode = "ProductCode";
        public static string Lot = "Lot";
        public static string InvalidDate = "InvalidDate";
        public static string SerialNumber = "SerialNumber";
        public static string Quantity = "Quantity";
        
    }


    public class Utils
    {
        
        public static DataTable ConvertListToDataTable<T>(IList<T> data)
        {
            if (data == null)
            {
                return null;
            }
            List<FieldInfo> targetFields = new List<FieldInfo>();
            DataTable dt = new DataTable();

            Type objType = typeof(T);
            ///取属性上的自定义特性  
            foreach (FieldInfo fieldInfo in objType.GetFields())
            {
                object[] objAttrs = fieldInfo.GetCustomAttributes(typeof(DataTableFieldAttribute), true);
                if (objAttrs.Length > 0)
                {
                    DataTableFieldAttribute attr = objAttrs[0] as DataTableFieldAttribute;
                    if (attr != null)
                    {
                        targetFields.Add(fieldInfo);
                        dt.Columns.Add(new DataColumn(fieldInfo.Name, typeof(string)));
                    }
                }
            }
            foreach (T obj in data)
            {
                DataRow dr = dt.NewRow();

                foreach (FieldInfo fieldInfo in targetFields)
                {
                    dr[fieldInfo.Name] = fieldInfo.GetValue(obj);
                }
                dt.Rows.Add(dr);
            }

            return dt;
        }

        public static DataGridTableStyle GetTableStyle(Type t)
        {
            DataGridTableStyle tableStyle = new DataGridTableStyle();

            DataGridColumnStyle columnStyle;

            foreach (FieldInfo fieldInfo in t.GetFields())
            {
                object[] objAttrs = fieldInfo.GetCustomAttributes(typeof(DataTableFieldAttribute), true);
                if (objAttrs.Length > 0)
                {
                    DataTableFieldAttribute attr = objAttrs[0] as DataTableFieldAttribute;
                    if (attr != null)
                    {
                        columnStyle = new DataGridTextBoxColumn();
                        columnStyle.HeaderText = attr.HeaderText;
                        columnStyle.MappingName = fieldInfo.Name;
                        columnStyle.NullText = "";
                        if (columnStyle.HeaderText == "产品代码" || columnStyle.HeaderText == "货位号")
                        {
                            columnStyle.Width = 100;
                        }
                        else if (columnStyle.HeaderText == "序列号")
                        {
                            columnStyle.Width = 150;
                        }
                        else
                        {
                            columnStyle.Width = attr.Width;
                        }
                        tableStyle.GridColumnStyles.Add(columnStyle);
                        
                    }
                }
            }

            return tableStyle;
        }

        public static Hashtable CheckBarcodeType(string barcode)
        {
            Hashtable result = new Hashtable();

            //HIBC first part
            if (barcode.StartsWith("+") && barcode.EndsWith("8")) 
            {
                result.Add(CheckBarcodeKeys.BarcodeType, ProductBarcodeType.HIBC_First);

                string productCode = barcode.Substring(1);
                productCode = productCode.Substring(0, productCode.Length - 1);
                result.Add(CheckBarcodeKeys.ProductCode, productCode);
            }
            //HIBC second part
            else if (barcode.StartsWith("+$$") && barcode.EndsWith("86")) 
            {
                result.Add(CheckBarcodeKeys.BarcodeType, ProductBarcodeType.HIBC_Second);
                string productCode = barcode.Substring(4);
                productCode = productCode.Substring(0, productCode.Length - 2);

                string quantity = productCode.Substring(0, 2);
                result.Add(CheckBarcodeKeys.Quantity, quantity);
                string invalidDate = productCode.Substring(2, 4);
                result.Add(CheckBarcodeKeys.InvalidDate, invalidDate);
                string lot = productCode.Substring(6);
                result.Add(CheckBarcodeKeys.Lot, lot);
            }

            else if (barcode.StartsWith("01") && (barcode.IndexOf("10") > 16 || barcode.IndexOf("17") > 16 || barcode.IndexOf("21") > 16))
            {
                result.Add(CheckBarcodeKeys.BarcodeType, ProductBarcodeType.GS1_Complete);

                string productCode = barcode.Substring(2, 14);
                result.Add(CheckBarcodeKeys.ProductCode, productCode);

                barcode = barcode.Substring(16);
                if (barcode.StartsWith("17"))
                {
                    string invalidDate = barcode.Substring(2, 6);
                    result.Add(CheckBarcodeKeys.InvalidDate, invalidDate);
                    barcode=barcode.Substring(8);
                }

                if (barcode.StartsWith("10"))
                {
                    string lot = barcode.Substring(2);
                    result.Add(CheckBarcodeKeys.Lot, lot);
                }

                if (barcode.StartsWith("21"))
                {
                    string serialNumber = barcode.Substring(2);
                    result.Add(CheckBarcodeKeys.SerialNumber, serialNumber);
                }
            }
            else if (barcode.StartsWith("01") && barcode.Length==16)
            {
                result.Add(CheckBarcodeKeys.BarcodeType, ProductBarcodeType.GS1_First);
                string productCode = barcode.Substring(2, 14);
                result.Add(CheckBarcodeKeys.ProductCode, productCode);
            }
            else if ((barcode.StartsWith("17") 
                || barcode.StartsWith("21")
                || barcode.StartsWith("10")) && barcode.Length>15)
            {
                result.Add(CheckBarcodeKeys.BarcodeType, ProductBarcodeType.GS1_Second);
                if (barcode.StartsWith("17"))
                {
                    string invalidDate = barcode.Substring(2, 6);
                    result.Add(CheckBarcodeKeys.InvalidDate, invalidDate);
                    barcode = barcode.Substring(8);
                }

                if (barcode.StartsWith("10"))
                {
                    string lot = barcode.Substring(2);
                    result.Add(CheckBarcodeKeys.Lot, lot);
                }

                if (barcode.StartsWith("21"))
                {
                    string serialNumber = barcode.Substring(2);
                    result.Add(CheckBarcodeKeys.SerialNumber, serialNumber);
                }
            }
            else
            {
                result.Add(CheckBarcodeKeys.BarcodeType, ProductBarcodeType.QR);
            }
            return result;
        }
    }
}
